package com.itboye.sunsun.luntan;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.Toast;

import com.itboye.sunsunhome.R;
import com.itboye.sunsun.adapter.LunTanHomePageAdapter;
import com.itboye.sunsun.application.App;
import com.itboye.sunsun.beans.CarouselPictureBean;
import com.itboye.sunsun.beans.LunTanHomePageBean;
import com.itboye.sunsun.beans.LunTanHomePageBean.LunTanItemBean;
import com.itboye.sunsun.beans.TieZiQuanXianBean;
import com.itboye.sunsun.constants.SPContants;
import com.itboye.sunsun.controller.LoginController;
import com.itboye.sunsun.custome.CarouselView;
import com.itboye.sunsun.custome.XListView;
import com.itboye.sunsun.custome.XListView.IXListViewListener;
import com.itboye.sunsun.login.ui.LoginActivity;
import com.itboye.sunsun.network.HttpRequest;
import com.itboye.sunsun.support.BaseActivity;
import com.itboye.sunsun.uesrinfo.TieZiQuanXianManager;
import com.itboye.sunsun.utils.SPUtils;
import com.itboye.sunsun.utils.XImageLoader;
import com.itboye.sunsun.volley.MyJsonRequest;
import com.itboye.sunsun.volley.XErrorListener;
import com.itboye.sunsun.volley.XRequestListener;
import com.itboye.sunsun.web.ui.WebActivity;

public class LunTanHomePageActivity extends BaseActivity {

	ImageView head;

	XListView xlistview;

	BaseAdapter adapter;

	ArrayList<Integer> luntanId = new ArrayList<>();
	CarouselView carouselView;
	List<LunTanItemBean> dataList = new ArrayList<>();
	List<LunTanItemBean> dataList2 = new ArrayList<>();
	ImageView message, message2;

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);

		setContentView(R.layout.activity_luntan_home_page);

		setStatusBarColor(R.color.top_blue);
		showProgressDialog("数据正在加载中，请稍后", true);
		IntentFilter filter = new IntentFilter("message");
		registerReceiver(broadcastReceiver, filter);
		if ((boolean) SPUtils.get(this, null, SPContants.IS_READ_ONE, false)
				|| (boolean) SPUtils.get(this, null, SPContants.IS_READ_TWO,
						false)) {
			message2.setVisibility(View.VISIBLE);
		}
		initView();

		getQuanXianInfo();

		getbanner();

		pullDown();
	}

	BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			// TODO Auto-generated method stub
			message2.setVisibility(View.VISIBLE);
		}
	};

	protected String uid = null;

	private void getQuanXianInfo() {
		MyJsonRequest.Builder<TieZiQuanXianBean> builder = new MyJsonRequest.Builder<TieZiQuanXianBean>()
				.apiVer("100")
				.typeKey("BY_BbsUserCenter_getUserInfo")

				.param("uid",
						(String) SPUtils.get(getApplicationContext(), null,
								SPContants.USER_ID, ""))
				.requestListener(new XRequestListener<TieZiQuanXianBean>() {

					@Override
					public void onResponse(TieZiQuanXianBean bean) {

						TieZiQuanXianManager.setUserInfo(bean);

						xlistview
								.setXListViewListener(new IXListViewListener() {

									@Override
									public void onRefresh() {
										pullDown();
									}

									@Override
									public void onLoadMore() {

									}

								});
					}
				}).errorListener(new XErrorListener() {

					@Override
					public void onErrorResponse(Exception exception, int code,
							String msg) {
						TieZiQuanXianManager.setUserInfo(null);

						pullDown();
						xlistview
								.setXListViewListener(new IXListViewListener() {

									@Override
									public void onRefresh() {
										pullDown();
									}

									@Override
									public void onLoadMore() {

									}

								});
					}
				});

		MyJsonRequest<TieZiQuanXianBean> request = builder.build();
		HttpRequest.getDefaultRequestQueue().add(request);
	}

	private void getbanner() {
		// TODO Auto-generated method stub
		MyJsonRequest<List<CarouselPictureBean>> request = new MyJsonRequest.Builder<List<CarouselPictureBean>>()
				.apiVer("100")
				.typeKey("BY_Picture_getURL")
				.param("position", "6057")
				.requestListener(
						new XRequestListener<List<CarouselPictureBean>>() {

							@Override
							public void onResponse(
									List<CarouselPictureBean> arg0) {
								List<ImageView> bmps = new ArrayList<ImageView>();
								for (CarouselPictureBean pictureBean : arg0) {
									ImageView imageView = new ImageView(App.ctx);

									imageView
											.setScaleType(ScaleType.CENTER_CROP);
									XImageLoader.load(pictureBean.getImgUrl(),
											imageView);
									bmps.add(imageView);
									imageView.setTag(pictureBean);
									imageView
											.setOnClickListener(new OnClickListener() {

												@Override
												public void onClick(View v) {
													// TODO Auto-generated
													// method stub
													CarouselPictureBean bean = (CarouselPictureBean) v
															.getTag();
													// 轮播图跳转类型 6070 链接 6071
													// app商品 6072 app帖子
													if ("http://sunsun.itboye.com/index.php/Webview/Game/index"
															.equals(bean
																	.getUrl())) {
														String uid = SPUtils
																.get(LunTanHomePageActivity.this,
																		null,
																		SPContants.USER_ID,
																		"")
																+ "";
														if (uid == null) {
															Intent intent = new Intent(
																	LunTanHomePageActivity.this,
																	LoginActivity.class);
															startActivity(intent);
														} else {
															Intent intent = new Intent(
																	App.ctx,
																	WebActivity.class);
															intent.putExtra(
																	"title",
																	"抽奖");
															intent.putExtra(
																	"url",
																	bean.getUrl()
																			+ "?uid="
																			+ uid
																			+ "&psw="
																			+ SPUtils
																					.get(LunTanHomePageActivity.this,
																							null,
																							SPContants.PASSWORD,
																							"")
																			+ "");
															startActivity(intent);
															System.out.println("抽奖链接"
																	+ bean.getUrl()
																	+ "?uid="
																	+ uid
																	+ "&psw="
																	+ SPUtils
																			.get(LunTanHomePageActivity.this,
																					null,
																					SPContants.PASSWORD,
																					"")
																	+ "");
														}
														return;
													}
													if ("6070".equals(bean
															.getUrlType())) {
														if (!bean.getUrl()
																.equals("")) {
															Intent intent = new Intent(
																	App.ctx,
																	WebActivity.class);
															intent.putExtra(
																	"title",
																	"商品详情");
															intent.putExtra(
																	"url",
																	bean.getUrl());
															startActivity(intent);
														}
													} else if ("6071".equals(bean
															.getUrlType())) {
//														Intent intent = new Intent(
//																getApplicationContext(),
//																GoodDetailActivity.class);
//														intent.putExtra("what",
//																bean.getUrl());
//														startActivity(intent);
													} else if ("6072".equals(bean
															.getUrlType())) {
														startActivity(new Intent(
																getApplicationContext(),
																TieZiDetailActivity.class)
																.putExtra(
																		"tid",
																		bean.getUrl()));

													}
												}
											});
								}

								carouselView.setImageBitmaps(bmps);
							}
						}).errorListener(new XErrorListener() {

					@Override
					public void onErrorResponse(Exception exception, int code,
							String msg) {

					}
				}).build();
		HttpRequest.getDefaultRequestQueue().add(request);
	}

	protected void pullDown() {
		MyJsonRequest<LunTanHomePageBean> request = new MyJsonRequest.Builder<LunTanHomePageBean>()
				.apiVer("100").typeKey("BY_ForumForum_query")//
				// .param("page_no", "10")
				// .param("page_size", "6")
				.requestListener(new XRequestListener<LunTanHomePageBean>() {
					@Override
					public void onResponse(LunTanHomePageBean bean) {
						if (bean.getCount() > 0) {
							dataList.clear();
							dataList.addAll(bean.getThreadList());
							dataList2.clear();
							dataList2.addAll(bean.getList());
							Iterator<LunTanItemBean> itr = dataList2.iterator();
							while (itr.hasNext()) {
								LunTanItemBean b = itr.next();
								if ("3".equals(b.getFid())) {
									itr.remove();
								}
							}
							adapter.notifyDataSetChanged();
						}

						xlistview.stopRefresh();
						try {

							closeProgressDialog();
						} catch (Exception e) {
							// TODO: handle exception
						}
					}
				}).errorListener(new XErrorListener() {

					@Override
					public void onErrorResponse(Exception exception, int code,
							String msg) {
						xlistview.stopRefresh();
						try {

							closeProgressDialog();
						} catch (Exception e) {
							// TODO: handle exception
						}
					}
				}).build();
		HttpRequest.getDefaultRequestQueue().add(request);
	}

	private void initView() {
		String uri = (String) SPUtils.get(App.ctx, null, SPContants.AVATAR_URL,
				"");
		XImageLoader.load(uri, (ImageView) findViewById(R.id.head));

		View v = getLayoutInflater().inflate(
				R.layout.item_luntan_home_apge_headerview, null);

		xlistview.addHeaderView(v);

		adapter = new LunTanHomePageAdapter(dataList,
				LunTanHomePageActivity.this);
		xlistview.setAdapter(adapter);
		carouselView = (CarouselView) findViewById(R.id.lunbo);

		v.findViewById(R.id.paihangbang).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View v) {
						Intent intent = new Intent(getApplicationContext(),
								LunTanDetailActivity.class);
						intent.putExtra("fid", "3");
						intent.putExtra("title", "森森智囊团");
						startActivity(intent);

					}
				});

		v.findViewById(R.id.zhinangtuan).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						Intent intent = new Intent(getApplicationContext(),
								LunTanDetailActivity.class);
						intent.putExtra("fid", "7");
						intent.putExtra("title", "森森采购师");
						startActivity(intent);

					}
				});

		v.findViewById(R.id.sheji).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(getApplicationContext(),
						LunTanDetailActivity.class);
				intent.putExtra("fid", "8");
				intent.putExtra("title", "森森设计师");
				startActivity(intent);
			}
		});

		v.findViewById(R.id.gouwupinglun).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						Intent intent = new Intent(getApplicationContext(),
								LunTanDetailActivity.class);
						intent.putExtra("fid", "15");
						intent.putExtra("title", "购物评论区");
						startActivity(intent);
					}
				});

		v.findViewById(R.id.shuizu).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(LunTanHomePageActivity.this,
						LunTanDetailActivity.class);
				if (dataList2 != null) {
					if (dataList2.size() > 0) {
						intent.putExtra("fid", dataList2.get(0).getFid());
						intent.putExtra("title", dataList2.get(0).getName());
						intent.putExtra("subforum", dataList2.get(0)
								.getSubForum());
						startActivity(intent);
					} else {
						Toast.makeText(LunTanHomePageActivity.this,
								"数据加载失败,请稍后重试", 0).show();
					}
				}
			}
		});

		v.findViewById(R.id.chongwu).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (dataList2 != null) {
					if (dataList2.size() > 0) {
						Intent intent = new Intent(LunTanHomePageActivity.this,
								LunTanDetailActivity.class);
						intent.putExtra("fid", dataList2.get(1).getFid());
						intent.putExtra("title", dataList2.get(1).getName());
						intent.putExtra("subforum", dataList2.get(1)
								.getSubForum());
						startActivity(intent);
					} else {
						Toast.makeText(LunTanHomePageActivity.this,
								"数据加载失败,请稍后重试", 0).show();
					}
				}
			}
		});

		v.findViewById(R.id.diaoyu).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (dataList2 != null) {
					if (dataList2.size() > 0) {
						Intent intent = new Intent(LunTanHomePageActivity.this,
								LunTanDetailActivity.class);
						intent.putExtra("fid", dataList2.get(2).getFid());
						intent.putExtra("title", dataList2.get(2).getName());
						intent.putExtra("subforum", dataList2.get(2)
								.getSubForum());
						startActivity(intent);
					} else {
						Toast.makeText(LunTanHomePageActivity.this,
								"数据加载失败,请稍后重试", 0).show();
					}
				}
			}
		});

		v.findViewById(R.id.lvyou).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (dataList2 != null) {
					if (dataList2.size() > 0) {
						Intent intent = new Intent(LunTanHomePageActivity.this,
								LunTanDetailActivity.class);
						intent.putExtra("fid", dataList2.get(3).getFid());
						intent.putExtra("title", dataList2.get(3).getName());
						intent.putExtra("subforum", dataList2.get(3)
								.getSubForum());
						startActivity(intent);
					} else {
						Toast.makeText(LunTanHomePageActivity.this,
								"数据加载失败,请稍后重试", 0).show();
					}
				}
			}
		});

		v.findViewById(R.id.zhiwu).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (dataList2 != null) {
					if (dataList2.size() > 0) {
						Intent intent = new Intent(LunTanHomePageActivity.this,
								LunTanDetailActivity.class);
						intent.putExtra("fid", dataList2.get(4).getFid());
						intent.putExtra("title", dataList2.get(4).getName());
						intent.putExtra("subforum", dataList2.get(4)
								.getSubForum());
						startActivity(intent);
					} else {
						Toast.makeText(LunTanHomePageActivity.this,
								"数据加载失败,请稍后重试", 0).show();
					}
				}
			}
		});
		findViewById(R.id.back).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});

		findViewById(R.id.search).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				startActivity(new Intent(getApplicationContext(),
						LunTanSouSuoActivity.class));
			}
		});

		message.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				uid=(String) SPUtils.get(getApplicationContext(), null,
						SPContants.USER_ID, "");
				if (uid == null) {
					Intent intent = new Intent(App.ctx, LoginActivity.class);
					startActivity(intent);
				} else {
					// LoginController.onBBSMessageClick(
					// LunTanHomePageActivity.this, null);
					Intent intent = new Intent(App.ctx, WoDeTieZiActivity.class);
					if (((boolean) SPUtils.get(App.ctx, null,
							SPContants.IS_READ_ONE, false)) == true) {
						intent.putExtra("current", 3);
						SPUtils.put(App.ctx, null, SPContants.IS_READ_ONE,
								false);
					} else if (((boolean) SPUtils.get(App.ctx, null,
							SPContants.IS_READ_TWO, false)) == true) {
						SPUtils.put(App.ctx, null, SPContants.IS_READ_TWO,
								false);
						intent.putExtra("current", 4);
					}else{
						intent.putExtra("current", 0);
					}
					startActivity(intent);
					if (((boolean) SPUtils.get(App.ctx, null,
							SPContants.IS_READ_ONE, false)) == false
							&& ((boolean) SPUtils.get(App.ctx, null,
									SPContants.IS_READ_TWO, false)) == false) {
						message2.setVisibility(View.GONE);
					}

				}
			}
		});

		xlistview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				position--;
				position--;
				Intent intent = new Intent(getApplicationContext(),
						TieZiDetailActivity.class);
				LunTanItemBean bean = dataList.get(position);
				// intent.putExtra("fid", bean.getFid());
				intent.putExtra("tid", bean.getTid());
				intent.putExtra("author", bean.getAuthor());
				intent.putExtra("author_id", bean.getAuthorId());
				intent.putStringArrayListExtra("img", bean.getImg());
				startActivity(intent);

			}
		});

		head.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				LoginController.onBBSMySelfClick(LunTanHomePageActivity.this,
						null);
			}
		});
	}

}
